import requests
import config
from lib.print_sql import PrintSQL
from lib.writexhxtoken import WriteXhxToken


class ProSQL:
    def __init__(self):
        self.urlsql = "https://xhx.kaike.la/database/selfterminal"
        self.session = requests.session()

    def sql_select(self):
        cookie = {"xhxtoken": "00002771_6b0eea14169c5190c7386c995aa31edc695"}
        sql = "select * FROM qb_exam_result_level where id = 66;"
        core = "estudy_s"
        data = {
            "sql": sql,
            "core": core,
            "is_exp": "0",
            "ext_type": "1"
        }
        respone = self.session.post(self.urlsql, data, cookies=cookie)
        config.prints(respone)
        try:
            response_json = respone.json()
            printer = PrintSQL()
            headers = response_json['data']['head']
            data = response_json['data']['body']
            printer.print_table(headers, data)

        except ValueError:
            print(response_json .text)

        finally:
            self.session.close()

ProSQL().sql_select()
